package com.yuwei.module_cash_desk

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.lib_http.entity.CashDeskEntityItem
import com.yuwei.module_cash_desk.adapter.CashDeskAdapter
import com.yuwei.module_cash_desk.databinding.ActivityCashDeskBinding
import com.yuwei.module_cash_desk.viewmodel.CashDeskViewModel
/**
 * 智慧园区-03-收银台-列表页面-V1.1-20250508-邓孟卓
 */
@Route(path = RouterPath.MODULE_CASH_DESK_ACTIVITY)
class CashDeskActivity : BaseMvvmActivity<ActivityCashDeskBinding>() {
    private val vm by viewModels<CashDeskViewModel>()
    private val cashDeskAdapter = CashDeskAdapter()
    private val cashList = ArrayList<CashDeskEntityItem>()
    override fun initViews() {
        vm.getCashDesk()
        binding.rv.adapter = cashDeskAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        cashDeskAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.MODULE_CASH_DESK_DETAIL_ACTIVITY).withSerializable("cashDesk",cashDeskAdapter.data[position]).navigation()
        }
        binding.cashBack.setOnClickListener {
            finish()
        }
        binding.editText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                cashDeskAdapter.data.clear()
                val items = cashList.filter {
                    it.payerName.contains(s.toString())
                }
                cashDeskAdapter.data.addAll(items)
                cashDeskAdapter.notifyDataSetChanged()

            }

            override fun afterTextChanged(s: Editable?) {
                if(s.isNullOrEmpty()){
                    cashDeskAdapter.data.clear()
                    cashDeskAdapter.data.addAll(cashList)
                    cashDeskAdapter.notifyDataSetChanged()
                }
            }

        })
    }
    override fun observeDataChange() {
        vm.getCashDeskSuccess.observe(this){
            cashList.addAll(it)
            cashDeskAdapter.data.addAll(it)
            cashDeskAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_cash_desk
}